<template>
  <div class="app-container">
    <div class="nav-search">
      <el-select v-model="searchParams.packageRuleType" placeholder="选择规则类型">
        <el-option
          v-for="item in dict.package_rule_type"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-input v-model="searchParams.searchObjStr" placeholder="搜索规则名称/医生患者名称/集团诊所名称"></el-input>
      <el-date-picker
        v-model="searchParams.creatDate"
        :clearable="false"
        type="date"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        placeholder="创建日期"
        clear-icon
      >
      </el-date-picker>
      <span>
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQueryRule">搜索</el-button>
      </span>
      <div class="tableHandle">
        <el-button class="filter-item" size="mini" type="success" @click="addMealRule">新增规则</el-button>
      </div>
    </div>
    <div class="tableList">
      <el-table
        ref="table"
        v-loading="loading"
        stripe
        :data="data"
        size="small"
        style="width: 100%;"
      >
        <el-table-column prop="id" label="规则编号" min-width="180"></el-table-column>
        <el-table-column prop="packageRuleType" label="规则类型" min-width="150">
          <template slot-scope="scope">
            {{ dict.label.package_rule_type[scope.row.packageRuleType] }}
          </template>
        </el-table-column>
        <el-table-column prop="packageNames" label="套餐名称" min-width="150"></el-table-column>
        <el-table-column prop="enabled" label="规则状态" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.enabled == 1 ? "启用" : "停用" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="ruleName" label="规则名称" min-width="150"></el-table-column>
        <el-table-column prop="createDate" label="创建时间" min-width="120"></el-table-column>
        <el-table-column prop="patientCount" label="患者" min-width="80">
          <template slot-scope="scope">
            <a href="javascript:void(0);" style="color: #2688B3;text-decoration: underline;" @click="linkSearch('/oper/patient/skip',scope.row.id)">{{ scope.row.patientCount }}</a>
          </template>
        </el-table-column>
        <el-table-column prop="doctorCount" label="医生" min-width="80">
          <template slot-scope="scope">
            <a href="javascript:void(0);" style="color: #2688B3;text-decoration: underline;" @click="linkSearch('/oper/doctor',scope.row.id)">{{ scope.row.doctorCount }}</a>
          </template>
        </el-table-column>
        <el-table-column prop="clinicCount" label="诊所" min-width="80">
          <template slot-scope="scope">
            <a href="javascript:void(0);" style="color: #2688B3;text-decoration: underline;" @click="linkSearch('/oper/clinic',scope.row.id)">{{ scope.row.clinicCount }}</a>
          </template>
        </el-table-column>
        <el-table-column prop="groupCount" label="集团" min-width="80">
          <template slot-scope="scope">
            <a href="javascript:void(0);" style="color: #2688B3;text-decoration: underline;" @click="linkSearch('/oper/bloc/skip',scope.row.id)">{{ scope.row.groupCount }}</a>
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作" min-width="150">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button v-if="scope.row.enabled == 1" size="mini" type="danger" @click="handleStop(scope.$index, scope.row)">停用</el-button>
            <el-button v-else size="mini" type="success" @click="handleStop(scope.$index, scope.row)">启用</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        background
        :page-size.sync="page.size"
        :total="total"
        :current-page.sync="page.page"
        style="margin-top: 8px; text-align: right;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChangeHandler($event)"
        @current-change="pageChangeHandler"
      ></el-pagination>
    </div>
    <add-rule ref="addRule" @addPackageRuleSuccess="addPackageRuleSuccess"></add-rule>
  </div>
</template>
<script>
import { queryRule, editPackageRule } from '@/api/packageRule';
import addRule from '@/views/meal/rule/addRule';
import { mapMutations } from 'vuex';
const storeModule = 'settings/';
export default {
  name: 'OperateMealRule',
  dicts: ['package_rule_type'],
  components: {
    addRule
  },
  data() {
    return {
      loading: false,
      searchParams: {
        packageRuleType: null, // 规则配置
        searchObjStr: null, // 模糊搜索
        creatDate: null // 创建时间
      },
      data: [],
      page: {
        size: 10,
        page: 1
      },
      total: 0
    };
  },
  computed: {},
  mounted() {
    this.queryLogRuleList({ pageIndex: 0, pageSize: 10 });
  },
  methods: {
    ...mapMutations(storeModule, ['CHANGE_LINKSEARCH']),
    // 搜索
    toQueryRule() {
      const search = {
        pageIndex: this.page.page - 1,
        pageSize: this.page.size,
        ...this.searchParams
      };
      this.queryLogRuleList(search);
    },
    // 新增规则
    addMealRule() {
      this.$refs.addRule.isShow();
    },
    // 新增/编辑 规则成功
    addPackageRuleSuccess() {
      this.page.page = 1;
      this.page.size = 10;
      this.toQueryRule();
    },
    // 编辑规则
    handleEdit(index, row) {
      this.$refs.addRule.isShow(row, true);
    },
    // 停用规则
    handleStop(index, row) {
      const params = JSON.parse(JSON.stringify(row));
      params.enabled = parseInt(params.enabled) === 1 ? 0 : 1;
      params.packageIds = JSON.parse(params.packageIds);
      params.rules = JSON.parse(params.rules);
      this.$confirm('此操作将停用, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        editPackageRule(params).then(res => {
          this.$notify.success(`${Number(params.enabled) === 1 ? '启用用成功' : '停用成功'}`);
          this.addPackageRuleSuccess();
        });
      });
    },
    sizeChangeHandler(item) {
      this.toQuery();
    },
    pageChangeHandler(item) {
      this.toQuery();
    },
    // 列表
    queryLogRuleList(params) {
      this.loading = true;
      queryRule(params).then(res => {
        this.data = res.content || [];
        this.total = parseInt(res.totalElements) || 0;
        this.loading = false;
      }).catch(er => {
        this.loading = false;
      });
    },
    // 列表链接跳转
    linkSearch(link, name) {
      this.CHANGE_LINKSEARCH(name);
      this.$router.push({ path: link });
    }
  }
};
</script>
<style scoped lang='scss'>
.nav-search{
  padding: 10px 0;
  .el-input{
    width: 320px;
    margin-right: 10px;
  }
  .el-date-editor{
    width: 150px;
  }
}
.tableHandle {
  float: right;
  margin: 0;
  padding: 0;
  margin-top: 4px;
}
</style>
